package com.shanzmoo.platform.tomember.api.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import com.shanzmoo.db.module.member.domain.vo.MemberAddressSaveDto;
import com.shanzmoo.db.module.member.domain.vo.MemberAddressUpdateDto;
import com.shanzmoo.db.module.member.domain.vo.MemberAddressVo;
import com.shanzmoo.db.module.member.entity.MemberAddressEntity;
import com.shanzmoo.db.module.member.entity.MemberInfoEntity;
import com.shanzmoo.db.module.member.entity.MemberMerchantEntity;
import com.shanzmoo.db.module.member.service.IMemberAddressService;
import com.shanzmoo.db.module.member.service.IMemberInfoService;
import com.shanzmoo.db.module.member.service.IMemberMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class MemberAddressManager {
    @Autowired
    private IMemberMerchantService memberMerchantService;
    @Autowired
    private IMemberInfoService memberInfoService;
    @Autowired
    private IMemberAddressService memberAddressService;

    @Transactional(rollbackFor = Exception.class)
    public SHttpResponse<Boolean> addAddress(String valueOf, MemberAddressSaveDto saveDto) {
        MemberAddressEntity addressEntity = saveDto.convert();
        MemberMerchantEntity memberMerchant = memberMerchantService.getById(valueOf);
        if (memberMerchant==null){
            return SHttpResponse.fail(50001,"商户会员不存在");
        }
        MemberInfoEntity memberInfoEntity = memberInfoService.getById(memberMerchant.getMemberInfoId());
        if (memberInfoEntity == null){
            return SHttpResponse.fail(50001,"会员不存在");
        }

        addressEntity.setMemberInfoId(memberInfoEntity.getId());

        if (saveDto.getDefaultAddress()) {
            //deleteDefault(memberId);
            memberAddressService.deleteDefault(addressEntity.getMemberInfoId());
        }
        memberAddressService.save(addressEntity);
        return SHttpResponse.ok(true);
    }

    public SHttpResponse<List<MemberAddressVo>> getAddressList(String valueOf) {
        MemberMerchantEntity memberMerchant = memberMerchantService.getById(valueOf);
        if (memberMerchant==null){
            return SHttpResponse.fail(50001,"商户会员不存在");
        }
        MemberInfoEntity memberInfoEntity = memberInfoService.getById(memberMerchant.getMemberInfoId());
        if (memberInfoEntity == null){
            return SHttpResponse.fail(50001,"会员不存在");
        }

        List<MemberAddressEntity> list = memberAddressService.listbyMemberId(memberInfoEntity.getId());
        List<MemberAddressVo> collect = list.stream().map(MemberAddressVo::create).collect(Collectors.toList());
        return SHttpResponse.ok(collect);
    }

    public SHttpResponse<Boolean> deleteById(Integer id, String valueOf) {
        MemberMerchantEntity memberMerchant = memberMerchantService.getById(valueOf);
        if (memberMerchant==null){
            return SHttpResponse.fail(50001,"商户会员不存在");
        }
        MemberInfoEntity memberInfoEntity = memberInfoService.getById(memberMerchant.getMemberInfoId());
        if (memberInfoEntity == null){
            return SHttpResponse.fail(50001,"会员不存在");
        }
        MemberAddressEntity addressEntity = obtainAddressEntityByMember(id, memberInfoEntity.getId());
        addressEntity.setDeleted(true);
        memberAddressService.updateById(addressEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }

    public SHttpResponse<Boolean> updateAddress(String valueOf, MemberAddressUpdateDto updateDto) {
        MemberMerchantEntity memberMerchant = memberMerchantService.getById(valueOf);
        if (memberMerchant==null){
            return SHttpResponse.fail(50001,"商户会员不存在");
        }
        MemberInfoEntity memberInfoEntity = memberInfoService.getById(memberMerchant.getMemberInfoId());
        if (memberInfoEntity == null){
            return SHttpResponse.fail(50001,"会员不存在");
        }

        obtainAddressEntityByMember(updateDto.getId(), memberInfoEntity.getId());

        if (updateDto.getDefaultAddress()) {
            memberAddressService.deleteDefault(memberInfoEntity.getId());
        }

        MemberAddressEntity addressEntity = updateDto.convert();
        memberAddressService.updateById(addressEntity);

        return SHttpResponse.ok(Boolean.TRUE);
    }
    /**
     * 根据 地址id 获取当前用户的地址
     * @param addressId
     * @return
     */
    private MemberAddressEntity obtainAddressEntityByMember(Integer addressId, Integer memberInfoId) {
        MemberAddressEntity addressEntity = memberAddressService.getById(addressId);
        if (addressEntity == null || !memberInfoId.equals(addressEntity.getMemberInfoId())){
            throw new RuntimeException("获取操作用户地址异常");
        }
        return addressEntity;
    }
}
